package com.yonyougov.devops.cruxcisupport.subscribe;

import com.yonyougov.devops.cruxcisupport.subscribe.dto.SubscribeCancelDto;
import com.yonyougov.devops.cruxcisupport.subscribe.dto.SubscribeCancelRequestDto;
import com.yonyougov.devops.cruxcisupport.subscribe.dto.SubscribeDto;
import com.yonyougov.devops.cruxcisupport.subscribe.dto.SubscribeRequestDto;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;
import java.util.List;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(tags = "订阅模块")
@RestController
@RequestMapping("/api/v2/subscribe")
public class SubscribeController extends CrudController<SubscribeEntity, BigInteger> {

    private final SubscribeService subscribeService;

    SubscribeController(SubscribeService subscribeService) {
        this.subscribeService = subscribeService;
    }

    @Override
    public BaseService<SubscribeEntity, BigInteger> getService() {
        return subscribeService;
    }

    //<!--用户自定义代码开始-->
    @ApiOperation(value = "前端应用列表展示调用", notes = "当前用户订阅的信息")
    @PostMapping(value = "/s/all")
    public BaseResponseDto findAppsOfCurrentUserSub() {
        List<SubscribeEntity> datas = subscribeService.findAppsOfCurrentUserSub();

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", datas);
        return result;
    }

    @ApiOperation(value = "前端应用列表订阅调用", notes = "当前用户批量订阅")
    @PostMapping(value = "/s/subscribe")
    public BaseResponseDto subscribe(@RequestBody @Valid SubscribeRequestDto request) {
        SubscribeDto subscribeDto = new SubscribeDto();
        BeanUtils.copyProperties(request, subscribeDto);

        subscribeService.subscribe(subscribeDto);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端应用列表取消订阅调用", notes = "当前用户批量取消订阅")
    @PostMapping(value = "/s/cancel")
    public BaseResponseDto cancelSubscribe(@RequestBody @Valid SubscribeCancelRequestDto request) {
        SubscribeCancelDto subscribeCancelDto = new SubscribeCancelDto();

        subscribeService.cancelSubscribe(subscribeCancelDto);

        return BaseResponseDto.getSuccessInstance();
    }
    //<!--用户自定义束-->
}
